﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Security.Cryptography;
using DevComponents.DotNetBar.Controls;
using DevComponents.DotNetBar;
using QLTuyenXe.BLL;
using QLTuyenXe.DAL;
using QLTuyenXe.DAO;
using QLTuyenXe.library;
using System.Data.SqlClient;

namespace QLTuyenXe.GUI
{
    public partial class frmPhanQuyen : DevComponents.DotNetBar.Office2007RibbonForm
    {
        BUser user = new BUser();
        USER users = new USER();
        BGroup group = new BGroup();
        XuLyDL xl = new XuLyDL();
        ThongBao thongBao = new ThongBao();
        int i = 0;
        public frmPhanQuyen()
        {
            InitializeComponent();
        }

        private void btnThemVaiTro_Click(object sender, EventArgs e)
        {

        }

        private void cboNhom_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btnNhom_Click(object sender, EventArgs e)
        {
            frmGroup group = new frmGroup();
            frmGroup.ten = quyen;
            group.ShowDialog();
        }

        private void btnThemNguoiDung_Click(object sender, EventArgs e)
        {
            int k=0;
            //string nh= cboNhom.SelectedItem.ToString().Split(':')[0].ToString();
            if ((txtName.Text != "") && (txtPass.Text != "") && (txtHoTen.Text != "") &&(txtNhapLaiPass.Text != "")&&(cboNhom.Text !=""))
            {
                if (CheckPass(txtPass.Text) == false)
                {
                    if(Kiemtra(txtPass.Text, txtNhapLaiPass.Text) == 0)
                    {
                        if (xl.TenDN(txtName.Text) == true && xl.HoTen(txtHoTen.Text) == true)
                        {
                            DataTable ten = user.Select_TenDN("name='" +xl.ChuanHoa(txtName.Text) + "'");
                            if(ten.Rows.Count >0)
                            {
                                thongBao.KiemTraTruocKhiThem();
                            }
                            else
                            {
                                try
                                {
                                    users.MaND = user.getOrder();
                                    users.HoTen = xl.ChuanHoa(txtHoTen.Text);
                                    users.Name = xl.Injection(txtName.Text); 
                                    users.Password = xl.GetMD5(txtPass.Text);
                                    //users.MaNhom = cboNhom.SelectedItem.ToString().Split(':')[0].ToString();
                                    users.MaNhom = cboNhom.SelectedValue.ToString();
                                    k = user.Insert(users);
                                    if (k > 0)
                                    {
                                        Clear();
                                        HienThi();
                                        thongBao.Them();
                                    }
                                    else
                                    {
                                        thongBao.LoiThem();                                        
                                    }
                                }
                                    catch (SqlException e1)
                                    {
                                        MessageBox.Show(e1.ToString());
                                        //MessageBoxEx.Show("Bạn chưa chọn nhóm cho người dùng", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    }
                            }
                        }
                        else
                        {
                            thongBao.ktHoTen_TenDN();
                        }
                   }
             }
            }
           
        }

        private void frmPhanQuyen_Load(object sender, EventArgs e)
        {
            Load_Nhom();
            Load_TKNhom();
            HienThi();
            CkeckQuyen();
            
        }
        // phan quyen
        public static string quyen = string.Empty;
        public void CkeckQuyen()
        {
            DataTable them = user.Select_Quyen("name='"+ quyen +"'", "ht_them='X'");
            DataTable sua = user.Select_Quyen("name='" + quyen + "'", "ht_sua='X'");
            DataTable xoa = user.Select_Quyen("name='" + quyen + "'", "ht_xoa='X'");
            DataTable nhom = user.Select_Quyen("name='" + quyen + "'", "nhom='X'");
            if (them.Rows.Count > 0)
            {
                btnThemNguoiDung.Enabled = true;
            }
            else
            {
                btnThemNguoiDung.Enabled = false;
            }

            if (sua.Rows.Count > 0)
            {
                BtnCapNhat.Enabled = true;
            }
            else
            {
                BtnCapNhat.Enabled = false;
            }

            if (xoa.Rows.Count > 0)
            {
                btnXoa.Enabled = true;
            }
            else
            {
                btnXoa.Enabled = false;
            }
            if (nhom.Rows.Count > 0)
            {
                btnNhom.Enabled = true;
            }
            else
            {
                btnNhom.Enabled = false;
            }
        }


        public void Load_Nhom()
        {
            DataTable tbNhom = user.Select_Nhom();
                /*for (int i = 0; i < tbNhom.Rows.Count; i++)
                {
                    cboNhom.Items.Add(tbNhom.Rows[i][0].ToString() + ":    " + tbNhom.Rows[i][1].ToString());
                }*/
            cboNhom.DataSource = tbNhom;
            cboNhom.DisplayMember = "tennhom";
            cboNhom.ValueMember = "maNhom";
        }
        //combobox nhóm trong tim kiếm
        public void Load_TKNhom()
        {
            DataTable tbNhom = user.Select_Nhom();
            /*cboTKNhom.DataSource = tbNhom;
            cboTKNhom.DisplayMember = "tennhom";
            cboTKNhom.ValueMember = "maNhom";
             */
            for (int i = 0; i < tbNhom.Rows.Count; i++)
            {
                cboTKNhom.Items.Add(tbNhom.Rows[i][1].ToString());
            }
        }

        // kiem tra nhap lai pass
        public int Kiemtra(string a, string b)
        {
            //int i = 0;
            if (String.Compare(a, b) != 0)
            {
                thongBao.KTNhapLaiMK();
                return i = 1;
            }
            else
            {
                return i = 0;
            }
        }

        private void cboNhom_TextChanged(object sender, EventArgs e)
        {
        }

        private void txtMaND_TextChanged(object sender, EventArgs e)
        {

        }
        public static string GetMD5(string str)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] bHash = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
            StringBuilder sbHash = new StringBuilder();
            foreach (byte b in bHash)
            {
                sbHash.Append(String.Format("{0:x2}", b));
            }
            return sbHash.ToString();
        }
        //Kiem tra  do dai mat khau
        public bool CheckPass( string pass)
        {
            if (pass.Length <= 3)
            {

                thongBao.KTMatKhau();
                return true;
            }
            else
            {
                return false;
            }
        }
        //hien thi
        public void HienThi()
        {
            ArrayList us = user.Select_User(null);
            DataTable tbNguoiDung = new DataTable();
            tbNguoiDung.Columns.Add("STT", typeof(string));
            tbNguoiDung.Columns.Add("Mã Người Dùng", typeof(string));
            tbNguoiDung.Columns.Add("Họ Tên", typeof(string));
            tbNguoiDung.Columns.Add("Tên Đăng Nhập", typeof(string));
            tbNguoiDung.Columns.Add("Nhóm", typeof(string));
            if (us.Count > 0)
            {
                foreach (USER u in us)
                {
                    DataRow row = tbNguoiDung.NewRow();
                    row["Mã Người Dùng"] = u.MaND;
                    row["Họ Tên"] = u.HoTen;
                    row["Tên Đăng Nhập"] = u.Name;
                    row["Nhóm"] = u.TenNhom;
                    tbNguoiDung.Rows.Add(row);
                }
                dgvNguoiDung.DataSource = tbNguoiDung;
                dgvNguoiDung.RowHeadersVisible = false;
                dgvNguoiDung.Columns[2].Visible = false;
                dgvNguoiDung.Columns[1].Width = 73;
                dgvNguoiDung.Columns[3].Width = 200;
                dgvNguoiDung.Columns[4].Width = 167;
                dgvNguoiDung.Columns[5].Width = 166;
                // stt
                int stt = 0;
                for (int i = 0; i < dgvNguoiDung.Rows.Count - 1; i++)
                {
                    stt = stt + 1;
                    dgvNguoiDung.Rows[i].Cells[1].Value = stt;
                }
                txtTong.Text = stt.ToString();
            }
        }

        private void btnLamMoi_Click(object sender, EventArgs e)
        {
            HienThi();
            cboNhom.DataSource = null;
            Load_Nhom();
            cboTKNhom.Items.Clear();
            Load_TKNhom();
            rbHoTen.Checked = false;
            rbTenDN.Checked =false;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBoxEx.Show("Bạn muốn xóa không?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                string key = "";
                int count = 0;
                int rcount_ = dgvNguoiDung.Rows.Count - 1;
                if (rcount_ <= 0) { return; }
                for (int i = rcount_; i >= 0; i--)
                {
                    if (dgvNguoiDung.Rows[i].Cells[0].Value != null)
                    {
                        key = dgvNguoiDung.Rows[i].Cells[2].Value.ToString();

                        count += user.Delete("maND='" + key + "'");
                    }
                }
                if (count > 0)
                {
                    HienThi();
                    thongBao.Kq_Xoa(count);
                }
                else
                {
                    thongBao.KTChonDongXoa();
                }
            }
        }

        private void btnChonAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dgvNguoiDung.Rows.Count - 1; i++)
            {
                dgvNguoiDung.Rows[i].Cells[0].Value = true;
            }
        }

        private void btnBoChonAll_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow r in dgvNguoiDung.Rows)
            {
                r.Cells[0].Value = false;
            }
        }

        private void dgvNguoiDung_Click(object sender, EventArgs e)
        {
            txtHoTen.Text = dgvNguoiDung.CurrentRow.Cells[3].Value.ToString();
            txtName.Text = dgvNguoiDung.CurrentRow.Cells[4].Value.ToString();
            cboNhom.Text = dgvNguoiDung.CurrentRow.Cells[5].Value.ToString();
            txtPass.Clear();
            txtNhapLaiPass.Clear();
        }

        private void BtnCapNhat_Click(object sender, EventArgs e)
        {
            int k = 0;
            
            //Trương hợp thay toàn bộ
            if ((txtPass.Text != "") && (txtNhapLaiPass.Text != ""))
            {
                if (Kiemtra(txtPass.Text, txtNhapLaiPass.Text) == 0)
                {
                    if (CheckPass(txtPass.Text) == false)
                    {
                        users.HoTen = xl.ChuanHoa(txtHoTen.Text);
                        users.Name = xl.ChuanHoa(txtName.Text);
                        users.Password = GetMD5(txtPass.Text);
                        // users.MaNhom = cboNhom.SelectedItem.ToString().Split(':')[0].ToString();
                        users.MaNhom = cboNhom.SelectedValue.ToString();
                        //string cu = dgvNguoiDung.Rows[0].Cells[4].Value.ToString();
                        string cu = dgvNguoiDung.CurrentRow.Cells[4].Value.ToString();
                        if (xl.TenDN(txtName.Text) == true && xl.soSanhChuoi(cu, txtName.Text.ToString())==true)
                        {
                                k = user.Update(users, "maND='" + dgvNguoiDung.CurrentRow.Cells[2].Value.ToString() + "'");
                                if (k > 0)
                                {
                                    HienThi();
                                    thongBao.Sua();
                                }
                                else
                                {
                                    thongBao.LoiSua();
                                }
                            }
                        }
                    }
                    }
            //Truong hop tahy doi tendnhap, ho ten, nhom
            if ((txtPass.Text == "") && (txtNhapLaiPass.Text == "")&& txtHoTen.Text !="" && txtName.Text !="" )
            {
                users.HoTen = xl.ChuanHoa(txtHoTen.Text);
                users.Name = txtName.Text;
                users.MaNhom = cboNhom.SelectedValue.ToString();
                if (xl.TenDN(txtName.Text) == true)
                {
                    DataTable ten = user.Select_TenDN("name='" + txtName.Text + "'");
                    if (ten.Rows.Count > 0)
                    {
                        thongBao.KiemTraTruocKhiThem();
                    }
                    else
                    {
                        k = user.Update_DoiNhom(users, "maND='" + dgvNguoiDung.CurrentRow.Cells[2].Value.ToString() + "'");
                        if (k > 0)
                        {
                            HienThi();
                            thongBao.Them();
                        }
                        else
                        {
                            thongBao.LoiThem();
                        }
                    }
                }
                else
                {
                    thongBao.ktHoTen_TenDN();
                }
            }
        }
        // lam clear
        public void Clear()
        {
            txtHoTen.Clear();
            txtName.Clear();
            txtPass.Clear();
            txtNhapLaiPass.Clear();
        }

        private void cboTKNhom_TextChanged(object sender, EventArgs e)
        {
            if (cboTKNhom.Text.ToString() != "")
            {
                TK("tennhom=N'" + @cboTKNhom.Text.ToString() + "'");
            }
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            if (txtTimKiem.Text != "")
            {
                if (rbHoTen.Checked == true)
                {
                    TK("hoten LIKE N'%" + xl.ChuanHoa(txtTimKiem.Text) + "%'");
                }
                else
                {
                    if (rbTenDN.Checked == true)
                    {
                        TK("name=N'"+ xl.ChuanHoa(txtTimKiem.Text) + "'");
                    }
                    else
                    {
                        thongBao.TimKiem();
                    }
                }
                
            }
        }

        public void TK(string dk)
        {
                ArrayList us1 = user.Select_User(dk);
                DataTable tbND = new DataTable();
                tbND.Columns.Add("STT", typeof(string));
                tbND.Columns.Add("Mã Người Dùng", typeof(string));
                tbND.Columns.Add("Họ Tên", typeof(string));
                tbND.Columns.Add("Tên Đăng Nhập", typeof(string));
                tbND.Columns.Add("Nhóm", typeof(string));
                if (us1.Count > 0)
                {
                    foreach (USER u in us1)
                    {
                        DataRow row = tbND.NewRow();
                        row["Mã Người Dùng"] = u.MaND;
                        row["Họ Tên"] = u.HoTen;
                        row["Tên Đăng Nhập"] = u.Name;
                        row["Nhóm"] = u.TenNhom;
                        tbND.Rows.Add(row);
                    }
                    dgvNguoiDung.DataSource = tbND;
                    dgvNguoiDung.RowHeadersVisible = false;
                    dgvNguoiDung.Columns[2].Visible = false;
                    dgvNguoiDung.Columns[1].Width = 80;
                    dgvNguoiDung.Columns[3].Width = 211;
                    dgvNguoiDung.Columns[4].Width = 167;
                    dgvNguoiDung.Columns[5].Width = 166;
                    // stt
                    int stt = 0;
                    for (int i = 0; i < dgvNguoiDung.Rows.Count - 1; i++)
                    {
                        stt = stt + 1;
                        dgvNguoiDung.Rows[i].Cells[1].Value = stt;
                    }
                    txtTong.Text = stt.ToString();
                }
                else
                {
                    thongBao.DKTimKiem();
                }
        }

    }
}
